import { Body, Controller, Delete, Get, Param, ParseIntPipe, Post, Put, UsePipes, ValidationPipe } from '@nestjs/common';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { UserRO } from './user.interface';
import { UserService } from './user.service';

@Controller('user')
export class UserController {
  constructor(private readonly userService: UserService) {}

  @Get(':id')
  async findMe(@Param('id', new ParseIntPipe()) id): Promise<UserRO> {
    return await this.userService.findById(id)
  }

  @Put(':id')
  async update(@Param('id', new ParseIntPipe()) id, @Body() userData: UpdateUserDto) {
    return await this.userService.update(id, userData)
  }

  @Delete(':id')
  async destroy(@Param('id', new ParseIntPipe()) id) {
    const number = await this.userService.remove(id)
    if (number > 0) {
      return {
        message: '删除用户成功',
        code: 200,
        errors: ''
      }
    }
  }

  @UsePipes(new ValidationPipe())
  @Post('add')
  async create(@Body() userData: CreateUserDto) {
    return this.userService.create(userData)
  }
}
